using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class AccountDAO
	{
		public const string ACCOUNT_GET_BY_ID = "AccountGetByID";
		public const string ACCOUNT_GET_ALL = "AccountGetAll";
		public const string ACCOUNT_INSERT = "AccountInsert";
		public const string ACCOUNT_UPDATE_BY_ID = "AccountUpdateByID";
		public const string ACCOUNT_DELETE_BY_ID = "AccountDeleteByID";

		public const string USERNAME = "UserName";
		public const string PASSWORD = "PassWord";
		public const string NAME = "Name";
		public const string PAGE = "Page";
		public const string ROLE = "Role";
		public const string ACTIVE = "Active";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public AccountDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public AccountDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(ACCOUNT_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, USERNAME, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetAccountFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<AccountDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(ACCOUNT_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<AccountDTO>();
				while (dataReader.Read())
				{
					var item = GetAccountFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(AccountDTO account)
		{
			var command = Database.GetStoredProcCommand(ACCOUNT_INSERT);
			try
			{
				Database.AddOutParameter(command, USERNAME, DbType.String, 36);
				Database.AddInParameter(command, PASSWORD, DbType.String, account.PassWord);
				Database.AddInParameter(command, NAME, DbType.String, account.Name);
				Database.AddInParameter(command, PAGE, DbType.String, account.Page);
				Database.AddInParameter(command, ROLE, DbType.Boolean, account.Role);
                Database.AddInParameter(command, ACTIVE, DbType.Boolean, account.Active);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, USERNAME).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(AccountDTO account)
		{
			var command = Database.GetStoredProcCommand(ACCOUNT_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, USERNAME, DbType.String, account.UserName);
				Database.AddInParameter(command, PASSWORD, DbType.String, account.PassWord);
				Database.AddInParameter(command, NAME, DbType.String, account.Name);
				Database.AddInParameter(command, PAGE, DbType.String, account.Page);
				Database.AddInParameter(command, ROLE, DbType.String, account.Role);
				Database.AddInParameter(command, ACTIVE, DbType.String, account.Active);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string username)
		{
			var command = Database.GetStoredProcCommand(ACCOUNT_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, USERNAME, DbType.String, username);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static AccountDTO GetAccountFromDataReader(IDataRecord dataReader)
		{
			var item = new AccountDTO();
			try
			{

				item.UserName = (string.IsNullOrEmpty(Convert.ToString(dataReader[USERNAME]))) ? "" : Convert.ToString(dataReader[USERNAME]);
				item.PassWord = (string.IsNullOrEmpty(Convert.ToString(dataReader[PASSWORD]))) ? "" : Convert.ToString(dataReader[PASSWORD]);
				item.Name = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAME]))) ? "" : Convert.ToString(dataReader[NAME]);
				item.Page = (string.IsNullOrEmpty(Convert.ToString(dataReader[PAGE]))) ? "" : Convert.ToString(dataReader[PAGE]);
				item.Role = (string.IsNullOrEmpty(Convert.ToString(dataReader[ROLE]))) ? false : Convert.ToBoolean(dataReader[ROLE].ToString());
				item.Active = (string.IsNullOrEmpty(Convert.ToString(dataReader[ACTIVE]))) ? false : Convert.ToBoolean(dataReader[ACTIVE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

